<?php

class account{
	
	var $usr;
	var $pwd;
	var $fullname;
	var $lastlog;
	var $db;
	
	// Class Constructor
	public function __construct(){
		$this->usr = '';
		$this->pwd = '';
		$this->db = new DBClass();
	}
	
	public function set_user($user){
		$this->usr = $user;
	}
	
	public function set_password($password){
		$this->pwd = $password;
	}
	
	public function do_login(){
		
		$query = "Select * from pengguna where email='$this->usr' and password=md5('$this->pwd')";
		$rows = $this->db->getRows($query);
				
		if($rows!=null){
			$this->set_cookie();
			return true;
		}else{
			return false;
		}
		
	}
	
	public function do_logout(){
		$this->delete_cookie();
	}

	public function get_data(){
		$query = "select idadmin, username, fullname, email from admin 
			where username='".$_COOKIE['c_usr']."'";

		$rows = $this->db->getRows($query);

		return $rows[0];	
	}
	
	protected function set_cookie(){
		setcookie("c_usr", $this->usr, time()+3600); 
		setcookie("c_pwd", md5($this->pwd), time()+3600); 
		setcookie("c_name", $this->fullname, time()+3600); 
		setcookie("c_log", $this->lastlog, time()+3600); 
	}
	
	public function get_cookie(){
		return $_COOKIE;
	}
	
	public function validate_cookie(){
		if(@$_COOKIE['c_usr'] == '' || @$_COOKIE['c_pwd'] == '')
			return false;
		else return true;
	}

	protected function delete_cookie(){
		setcookie("c_usr", ''); 
		setcookie("c_pwd", ''); 
		setcookie("c_name", ''); 
		setcookie("c_log", ''); 
	}

}